package cn.shengchao.job.gankao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.shengchao.examstar.product.ohs.local.appservice.CourseService;
import cn.shengchao.examstar.product.ohs.local.appservice.ProductAppService;
import cn.shengchao.examstar.product.ohs.local.message.KnowledgeNodeDto;
import cn.shengchao.examstar.product.ohs.remote.rpc.CourseNodesRpc;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.common.log.SnailJobLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * @program: examstarapi
 * @description: 赶考任务的加载处理
 * @author: Xiong weiqiang
 * @create: 2025-06-23 17:22
 */
@Component
public class GanKaoCourseBatchWorkerJob {
    @Autowired
    private CourseNodesRpc courseNodesRpc;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ProductAppService productAppService;

    @JobExecutor(name = "GanKaoCourseBatchStatusOn")
    public void processGanKaoCourseBatchStatusOn(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (StrUtil.isNotBlank(params)&& JSONUtil.isTypeJSON( params)){
            String courseIdPrefix= JSONUtil.parseObj( params).getStr("courseIdPrefix");;
            String specTag= JSONUtil.parseObj( params).getStr("specTag");
            Integer status= JSONUtil.parseObj( params).getInt("status");
            if (StrUtil.isNotBlank(courseIdPrefix)&&courseIdPrefix.length()>9&& status!=null&&List.of(1,2).contains( status)){
                List<Long> courseIds =courseService.findGkCourseIdBySpecTag(courseIdPrefix,specTag);
                if (CollUtil.isNotEmpty(courseIds)){
                    SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务:course size=" +courseIds.size());
                    for (Long courseId : courseIds){
                        List<KnowledgeNodeDto> knowledgeList = courseService.getKnowledgeList(courseId, "", false);
                        CompletableFuture.runAsync(() -> productAppService.updateCoursePrice(knowledgeList.stream().filter(a -> a.getIsLesson() != null && a.getIsLesson() == 1L).toList(), courseId));
                        courseService.updateCourseStatus(courseId,status);
                    }
                }else {
                    SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务:" + "没有需要处理的任务");
                }
            }else {
                SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务:" + "参数错误："+params);
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务:" + "参数不正确");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理刷新赶考课程以及启用任务：耗时："+(endTime-startTime)+"毫秒");
    }


}